package com.example.back.controller;

import com.example.back.model.Boardgame;
import com.example.back.model.Product;
import com.example.back.service.ProductService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品控制层
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    private static final Logger logger = LogManager.getLogger(ProductController.class);

    @Resource
    private ProductService productService;

    @RequestMapping("/getProductList")
    @ResponseBody
    public List<Product> getBoardgameList(){
        return productService.findAllProduct();
    }

    @RequestMapping("insertProduct")
    public String insertProduct(String name, double price, String unit, String type) {
        String rs = "";
        if (productService.insertProduct(name, price, unit, type)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>新商品添加成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>新商品添加失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("updateProduct")
    public String updateProduct(int id, double price) {
        String rs = "";
        if (productService.updateProduct(id, price)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>商品价格更新成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>商品价格更新失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("deleteProduct")
    public String deleteProduct(int id) {
        String rs = "";
        if (productService.deleteProduct(id)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>商品删除成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>商品删除失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("watchGPhoto")
    public String watchGPhoto(int id){
        return "<img src='" + productService.findPhotoByID(id) + "'>";
    }
}
